package com.ordersystem.controller;

import com.ordersystem.domain.Employee;
import com.ordersystem.domain.EmployeeDistribution;
import com.ordersystem.domain.Result;
import com.ordersystem.domain.UpdateEmployeeImg;
import com.ordersystem.service.EmployeeService;
import com.ordersystem.utils.PageUtils;
import com.ordersystem.utils.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : EmployeeController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/25 21:00]
 * @updateUser : asus1020
 * @updateTime : [2022/3/25 21:00]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    private PageUtils pageUtils=new PageUtils();
    private String checkCode;
    @Autowired
    private UploadFileUtils uploadFileUtils;
    @RequestMapping("/getCheckCode")
    public void getCheckCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //服务器通知浏览器不要缓存
        response.setHeader("pragma","no-cache");
        response.setHeader("cache-control","no-cache");
        response.setHeader("expires","0");

        //在内存中创建一个长80，宽30的图片，默认黑色背景
        //参数一：长
        //参数二：宽
        //参数三：颜色
        int width = 80;
        int height = 30;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //获取画笔
        Graphics g = image.getGraphics();
        //设置画笔颜色为灰色
        g.setColor(Color.GRAY);
        //填充图片
        g.fillRect(0,0, width,height);

        //产生4个随机验证码，12Ey
        String checkCode = getCheckCode();
        //将验证码放入HttpSession中
        this.checkCode=checkCode;
        request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);

        //设置画笔颜色为黄色
        g.setColor(Color.YELLOW);
        //设置字体的小大
        g.setFont(new Font("黑体",Font.BOLD,24));
        //向图片上写入验证码
        g.drawString(checkCode,15,25);

        //将内存中的图片输出到浏览器
        //参数一：图片对象
        //参数二：图片的格式，如PNG,JPG,GIF
        //参数三：图片输出到哪里去
        ImageIO.write(image,"PNG",response.getOutputStream());
    }
    @GetMapping("/checkCode")
    public Result checkCode(HttpSession session, String checkCode, HttpServletRequest request){
        Result result = new Result();

        if (this.checkCode==null){
            result.setStatus(500);
            result.setMessage("验证失败");
        }else {
            if (this.checkCode.equalsIgnoreCase(checkCode)){
                result.setStatus(200);
                result.setMessage("验证成功");
            }else {
                result.setStatus(500);
                result.setMessage("验证失败");
            }

        }
        return result;
    }
    @GetMapping("/findEmployee")//查询员工列表
    public Result getEmployees(String query, Integer pagenum, Integer pagesize){
        Result result = new Result();
        Map employee = employeeService.findEmployee(query, pagenum, pagesize);
        if (pagenum==null){
            pagenum=1;
        }
        if (pagesize==null){
            pagesize=10;
        }
        if (employee!=null){
            result.setData(employee);
            result.setMessage("查询员工列表成功");
            result.setStatus(200);
        }else {
            result.setStatus(500);
            result.setMessage("查询员工列表失败");
        }
        return result;
    }

    @PutMapping("/updateStatus/{eid}")//修改员工状态
    public Result updateStatus(@PathVariable("eid") Integer eid, boolean stastus){
        Result result = new Result();
        int total = employeeService.updateEmployeeStatus(eid, stastus);
        if (total>0){
            result.setStatus(200);
            result.setMessage("修改员工状态成功");
        }else {
            result.setStatus(500);
            result.setMessage("修改员工状态失败");
        }
        return result;
    }

    @PutMapping("/updateEmployee")//修改员工信息
    public Result updateEmployee(@RequestBody Employee employee, Integer rid){
        Result result = new Result();
        int total = employeeService.updateEmployee(employee,rid);
        if (total>0){
            result.setStatus(200);
            result.setMessage("修改员工信息成功");
        }else {
            result.setStatus(500);
            result.setMessage("修改员工信息失败");
        }
        return result;
    }

    @DeleteMapping("/deleteEmployee/{eid}")
    public Result deleteEmployee(@PathVariable("eid") Integer eid){
        Result result = new Result();
        int total = employeeService.deleteEmployee(eid);
        if (total>0){
            result.setStatus(200);
            result.setMessage("删除员工成功");
        }else {
            result.setStatus(500);
            result.setMessage("删除员工失败");
        }
        return result;
    }

    @PostMapping("/addEmployee/{rid}")
    public Result addEmployee(@RequestBody Employee employee, @PathVariable("rid") Integer rid){
        Result result = new Result();
        int total = employeeService.addEmployee(employee, rid);
        if (total>0){
            result.setStatus(200);
            result.setMessage("添加员工成功");
        }else {
            result.setStatus(500);
            result.setMessage("添加员工失败");
        }
        return result;
    }
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee){
        Result result = new Result();
        Employee login = employeeService.login(employee.getUsername(), employee.getPassword());
        if (login!=null){
            result.setStatus(200);
            result.setMessage("登录成功");
            result.setData(login);
        }else {
            result.setStatus(500);
            result.setMessage("登录失败");
        }

        return result;
    }
    @PostMapping("/updateHeaderImage")
    public Result updateHeaderImage(
            @RequestBody UpdateEmployeeImg updateEmployeeImg){
        Result result = new Result();
        int total = employeeService.updateEmployeeHeaderImage(updateEmployeeImg);
        if (total>0){
            result.setStatus(200);
            result.setMessage("修改成功");
        }else {
            result.setStatus(500);
            result.setMessage("修改失败");
        }
        return result;
    }
    private String getCheckCode() {
        String base = "0123456789ABCDEFGabcdefg";
        int size = base.length();
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=1;i<=4;i++){
            //产生0到size-1的随机值
            int index = r.nextInt(size);
            //在base字符串中获取下标为index的字符
            char c = base.charAt(index);
            //将c放入到StringBuffer中去
            sb.append(c);
        }
        return sb.toString();
    }
    @GetMapping("/getDistributionData")
    public Result getDistributionData(){
        Result result = new Result();
        List<EmployeeDistribution> employeeDistribution = employeeService.getEmployeeDistribution();
        if (employeeDistribution!=null){
            result.setStatus(200);
            result.setData(employeeDistribution);
        }else {
            result.setStatus(500);
            result.setMessage("获取数据失败");
        }
        return result;
    }
}
